# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface

import pymysql
import redis
from itemadapter import ItemAdapter

from scrapy.exceptions import DropItem

class ScrapyTestPipeline:
    def process_item(self, item, spider):
        return item


class RedisPipeline(object):
    def open_spider(self, spider):
        self.r = redis.Redis(host='127.0.0.1')

    def close_spider(self, spider):
        self.r.close()
    def process_item(self, item, spider):
        if self.r.sadd(spider.name,item['course_url']):
            return item
        # return DroupItemAdapter(item, spider)
        raise DropItem("Missing some_field")


class MysqlPipeline(object):
    def open_spider(self, spider):
        self.conn = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            db='py',
            user='root',
            password='root',
            charset='utf8',
        )
        self.cur = self.conn.cursor()

    def close_spider(self, spider):
        self.cur.close()
        self.conn.close()
    def process_item(self, item, spider):
        keys, values = zip(*item.items())
        sql = "insert into `{}` ({}) values ({})".format(
            item.table,
            ','.join(keys),
            ','.join(['%s'] * len(values))
        )
        self.cur.execute(sql,values)
        self.conn.commit()
        # print(self.cur._last_executed)
        return item
